//
//  SelectVipCardClassViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/30.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  选择会员卡种类

#import "SelectVipCardClassViewController.h"
#import "CardTableViewCell.h"
#import "VipCardFormatViewController.h"

@interface SelectVipCardClassViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    int startIndex;
    
    int pageSize;
}

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation SelectVipCardClassViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = BackGroundColor;
    
    self.title = @"选择会员卡种类";
    
    [self createSubView];
}

#pragma mark 创建子视图
-(void)createSubView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[CardTableViewCell class] forCellReuseIdentifier:@"CardTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = CSClearColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
    tableView.mj_header = mj_head;
    [tableView.mj_header beginRefreshing];
}

#pragma mark 下拉
-(void)loadStartData{
    
    startIndex = 1;
    
    pageSize = 20;
    
    [self loadData];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    startIndex = startIndex + 1;
    
    [self loadData];
}

#pragma mark 加载数据
-(void)loadData{
    
    //加载数据
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    BrandModel *brand = [BrandModel brand];
    
//    [params setObject:brand.brandID forKey:@"BrandID"];
//
//    [params setObject:[NSString stringWithFormat:@"%d",startIndex] forKey:@"page"];
//
//    [params setObject:[NSString stringWithFormat:@"%d",pageSize] forKey:@"rows"];
    
    [HTTPTool GET:Get_MemberListCard parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count > 0) {
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshing];
                  }
                  else{
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  if (startIndex == 1) {
                      
                      self.dataSource = [NSMutableArray arrayWithArray:resultArray];
                  }
                  else{
                      
                      [self.dataSource addObjectsFromArray:resultArray];
                  }
                  
//                  self.countLabel.text = [NSString stringWithFormat:@"%ld",self.dataSource.count];
                  
                  [self.tableView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
    
}

#pragma mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.00001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CardTableViewCell"];
    
    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
    
    CommonCard *commonCard = [CommonCard mj_objectWithKeyValues:dict];
    
    cell.commonCard = commonCard;
    
    cell.selectRow = indexPath.row;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VipCardFormatViewController *vipCardFormatVC = [[VipCardFormatViewController alloc] init];
    
    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
    
    CommonCard *commonCard = [CommonCard mj_objectWithKeyValues:dict];
    
    vipCardFormatVC.selectRow = indexPath.row;
    
    vipCardFormatVC.commonCard = commonCard;
    
    vipCardFormatVC.from = @"add";
    
    [self.navigationController pushViewController:vipCardFormatVC animated:YES];
}

#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
        
    }
    
    return _dataSource;
}

@end
